package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.AddressBookMapper;
import org.example.model.address.AddressBook;
import org.example.service.AddressBookService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 地址管理
 */
@Service
@Slf4j
@Transactional
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 添加地址
     *
     * @param addressBook
     */
    @Override
    public Boolean InsertAddressBook(AddressBook addressBook) {
        if (addressBook == null) return false;
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        addressBook.setUserId(userId);
        addressBook.setCreateDate(LocalDateTime.now());
        addressBook.setUpdateDate(LocalDateTime.now());
        addressBook.setIsDeleted(false);
        return addressBookMapper.insert(addressBook) > 0;
    }

    /**
     * 根据token中的用户id获取地址列表
     */
    @Override
    public List<AddressBook> SelectAddressBooksByUserId() {
        List<AddressBook> addressBooks = new ArrayList<>();
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return addressBooks;
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::isIsDeleted,false)
                .eq(AddressBook::getUserId,userId)
                .orderByDesc(AddressBook::getIsDefault)
                .orderByDesc(AddressBook::getCreateDate);
        addressBooks = addressBookMapper.selectList(queryWrapper);
        return addressBooks;
    }

    /**
     * 根据id查询地址详情(原始数据)
     *
     * @param id
     */
    @Override
    public AddressBook SelectAddressBookById(int id) {
        if (id <= 0) return new AddressBook();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::isIsDeleted,false)
                .eq(AddressBook::getId,id);
        return addressBookMapper.selectOne(queryWrapper);
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    @Override
    public Boolean UpdateAddressBookById(AddressBook addressBook) {
        if (addressBook == null) return false;
        AddressBook addressBookById = this.SelectAddressBookById(addressBook.getId());
        addressBookById.setName(addressBook.getName());
        addressBookById.setPhone(addressBook.getPhone());
        addressBookById.setProvinceCode(addressBook.getProvinceCode());
        addressBookById.setProvinceName(addressBook.getProvinceName());
        addressBookById.setCityName(addressBook.getCityName());
        addressBookById.setDistrictName(addressBook.getDistrictName());
        addressBookById.setDetail(addressBook.getDetail());
        addressBookById.setIsDefault(addressBook.getIsDefault());
        addressBookById.setIsDeleted(addressBook.isIsDeleted());
        addressBookById.setUpdateDate(LocalDateTime.now());
        return addressBookMapper.updateById(addressBookById) > 0;
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Override
    public Boolean DeleteAddressBookById(int id) {
        if (id <= 0) return false;
        AddressBook addressBook = this.SelectAddressBookById(id);
        addressBook.setIsDeleted(true);
        return this.UpdateAddressBookById(addressBook);
    }
}
